<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2019/4/26
 * Time: 16:51
 */

namespace app\api\controller\user;


use app\api\controller\BasicUserApi;
use app\store\service\MemberService;
use app\store\service\OrderService;
use service\KdniaoService;
use service\WechatService;
use think\Db;

class Index extends BasicUserApi
{
    /**
     * 用户推广二维码
     * @return \think\response\Json
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     */
    function qr_code(){
        $file='/static/upload/qr_code/'.UID.'.jpg';
        if(!file_exists('.'.$file)){
            $sence=UID;
            $page='pages/login/login';
            $res=WechatService::WeMiniQrcode()->createMiniScene($sence,$page);
            file_put_contents('.'.$file,$res);
        }
        return json(['msg'=>'success','data'=>$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$file]);
    }

    /**
     * 获取个人中心用户数据
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function info(){
        $member=Db::name('store_member')
            ->where('id',UID)
            ->field('nickname,headimg,type,point')
            ->find();
        return json(['msg'=>'succecss','data'=>$member]);
    }

    /**
     * 积分明细
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function point_log(){
        $page_now=input('get.page_now/d',1);
        $page_size=input('get.page_size/d',10);
        $account_log=Db::name('account_log')
            ->where('mid',UID)
            ->field('point,remark,FROM_UNIXTIME(addtime,\'%Y/%m/%d %H:%i\') addtime')
            ->page($page_now,$page_size)
            ->select();
        return json(['data'=>$account_log,'msg'=>'success']);
    }

    /**
     * 订单列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function order_list(){
        $status=input('get.status/d');
        $page_now=input('get.page_now/d',1);
        $page_size=input('get.page_size/d',10);
        $db=Db::name('store_order')
            ->alias('o')
            ->join('store_order_goods og','og.order_no=o.order_no')
            ->where('o.mid',UID);
        if($status){
            $db->where('o.status',$status);
        }
        $list=$db->field('o.order_no,o.status,og.goods_title,og.goods_logo,og.selling_price,og.number')
            ->page($page_now,$page_size)
            ->select();
        foreach ($list as &$v){
            $v['status_text']=OrderService::STATUS_TEXT[$v['status']];
        }
        return json(['msg'=>'success','data'=>$list]);
    }

    /**
     * 订单详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function order_view(){
        $order_no=input('get.order_no');
        $order=Db::name('store_order')
            ->alias('o')
            ->join('store_order_goods og','og.order_no=o.order_no')
            ->join('store_order_express oe','oe.order_no=o.order_no and oe.type = 0')
            ->where('o.order_no',$order_no)
            ->field('o.order_no,o.create_at,o.status,og.goods_title,og.goods_logo,og.selling_price,og.number,oe.username,oe.phone,oe.province,oe.city,oe.area,oe.address')
            ->find();
        if($order){
            $order['status_text']=OrderService::STATUS_TEXT[$order['status']];
        }
        return json(['msg'=>'success','data'=>$order]);
    }

    /**
     * 确定收货
     * @return \think\response\Json
     */
    function order_receive(){
        $order_no=input('get.order_no');
        Db::table('store_order')->where(['order_no' => $order_no,'mid' => UID,'is_deleted' => '0','status' => '3'])->setField('status','5');
        return json(['msg'=>'收货成功']);
    }

    /**
     * 订单物流
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function order_logistics(){
        $order_no=$this->request->get('order_no');
        $type=$this->request->get('type',0);
        //查询快递记录
        $orderExpress=Db::name('store_order_express')
            ->where('order_no',$order_no)
            ->where('type',$type)
            ->field('order_no,send_no,send_company_title,send_company_code')
            ->find();
        if(!$orderExpress){
            return json(['msg'=>'快递单查询失败'],404);
        }

        $kdniaoService=KdniaoService::getInstance();

        $param['ShipperCode']=$orderExpress['send_company_code'];
        $param['LogisticCode']=$orderExpress['send_no'];

        $res=$kdniaoService->getOrderTracesByJson($param);
        array_multisort(array_column($res['Traces'],'AcceptTime'),SORT_DESC,$res['Traces']);
        $res['Shipper']=$orderExpress['send_company_title'];
        return json(['msg'=>'success','data'=>$res]);
    }


}